package com.atmyc.yygh.hospital.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atmyc.cmn.client.DictFeignClient;
import com.atmyc.yygh.hospital.repository.HospitalRepository;
import com.atmyc.yygh.hospital.service.HospitalService;
import com.atmyc.yygh.model.hosp.Hospital;
import com.atmyc.yygh.vo.hosp.HospitalQueryVo;
import com.atmyc.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImp implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //判断是否存在数据
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString,Hospital.class);
        //如果不存在，进行添加
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        //如果存在，进行修改
        if(null != targetHospital) {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        } else {
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHospcode(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //创建pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建条件匹配器
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo,hospital);
        //创建对象
        Example<Hospital> example = Example.of(hospital,matcher);
        //调用方法实现查询
        Page<Hospital> all = hospitalRepository.findAll(example,pageable);
        all.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
            System.out.println(item);
        });
        
        return all;
    }

    @Override
    public void updateHospital(String id, Integer status) {
        if(status==0 || status==1){
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> show(String id) {
        Map<String,Object> result = new HashMap<>();

        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());

        result.put("bookingRule",hospital.getBookingRule());
        hospital.setBookingRule(null);
        result.put("hospital",hospital);
        return result;
    }

    private Hospital setHospitalHosType(Hospital item) {
        String hostype = dictFeignClient.getName("Hostype", item.getHostype());
        //查询省市地区
        String provinceString = dictFeignClient.getName(item.getProvinceCode());
        String cityString = dictFeignClient.getName(item.getCityCode());
        String doatrictString = dictFeignClient.getName(item.getDistrictCode());
        item.getParam().put("hostypeString",hostype);
        item.getParam().put("fullAddress",provinceString+cityString+doatrictString);
        return item;
    }


}
